/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JPnlAccordionSegments.java
 *
 * Created on 07/03/2010, 17:03:24
 */
package systole.view.accordion;

import java.util.Observable;
import java.util.Observer;
import org.jdesktop.application.Action;
import systole.view.tabs.controllers.AnalysisController;
import systole.view.tabs.controllers.SelectionItem;

/**
 *
 * @author jmj
 */
public class JPnlAccordionSegments extends javax.swing.JPanel implements Observer {

    /**
     *
     */
    private static final long serialVersionUID = -8073292683976998755L;
    private AnalysisController analysisControl;
    private Integer amount = 0;

    /** Creates new form JPnlAccordionSegments
     * @param current 
     */
    public JPnlAccordionSegments(AnalysisController logic) {
        initComponents();
        this.analysisControl = logic;
        this.analysisControl.getSelectionModel().addObserver(this);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPnlBottom = new javax.swing.JPanel();
        jBtnCofirm = new javax.swing.JButton();
        jBtnClear = new javax.swing.JButton();
        jPnlTop = new javax.swing.JPanel();
        jPnlLabels = new javax.swing.JPanel();
        jPnlSeg = new javax.swing.JPanel();
        lblCandidatesNumber = new javax.swing.JLabel();
        lblTotalSegments = new javax.swing.JLabel();
        jLblSelected = new javax.swing.JLabel();
        jLblTotal = new javax.swing.JLabel();
        jPnlTopCenter = new javax.swing.JPanel();
        jPnlBottomCenter = new javax.swing.JPanel();
        jPnlRigthCenter = new javax.swing.JPanel();
        jPnlLeftCenter = new javax.swing.JPanel();
        jPnlBtns = new javax.swing.JPanel();
        jBtnSegmentMap = new javax.swing.JButton();
        jBtnSegmentFlow = new javax.swing.JButton();

        setMinimumSize(new java.awt.Dimension(0, 0));
        setName("Form"); // NOI18N
        setPreferredSize(new java.awt.Dimension(140, 300));
        setLayout(new java.awt.BorderLayout());

        jPnlBottom.setName("jPnlBottom"); // NOI18N
        jPnlBottom.setPreferredSize(new java.awt.Dimension(140, 105));

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(systole.view.SystoleApp.class).getContext().getActionMap(JPnlAccordionSegments.class, this);
        jBtnCofirm.setAction(actionMap.get("confirmSelection")); // NOI18N
        jBtnCofirm.setFont(jBtnCofirm.getFont().deriveFont(jBtnCofirm.getFont().getSize()-2f));
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(systole.view.SystoleApp.class).getContext().getResourceMap(JPnlAccordionSegments.class);
        jBtnCofirm.setIcon(resourceMap.getIcon("jBtnCofirm.icon")); // NOI18N
        jBtnCofirm.setText(resourceMap.getString("jBtnCofirm.text")); // NOI18N
        jBtnCofirm.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jBtnCofirm.setMaximumSize(new java.awt.Dimension(120, 35));
        jBtnCofirm.setMinimumSize(new java.awt.Dimension(0, 0));
        jBtnCofirm.setName("jBtnCofirm"); // NOI18N
        jBtnCofirm.setPreferredSize(new java.awt.Dimension(120, 35));
        jPnlBottom.add(jBtnCofirm);

        jBtnClear.setAction(actionMap.get("clearSegments")); // NOI18N
        jBtnClear.setFont(jBtnClear.getFont().deriveFont(jBtnClear.getFont().getSize()-2f));
        jBtnClear.setIcon(resourceMap.getIcon("jBtnClear.icon")); // NOI18N
        jBtnClear.setText(resourceMap.getString("jBtnClear.text")); // NOI18N
        jBtnClear.setToolTipText(resourceMap.getString("jBtnClear.toolTipText")); // NOI18N
        jBtnClear.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jBtnClear.setMaximumSize(new java.awt.Dimension(120, 35));
        jBtnClear.setMinimumSize(new java.awt.Dimension(0, 0));
        jBtnClear.setName("jBtnClear"); // NOI18N
        jBtnClear.setPreferredSize(new java.awt.Dimension(120, 35));
        jPnlBottom.add(jBtnClear);

        add(jPnlBottom, java.awt.BorderLayout.CENTER);

        jPnlTop.setName("jPnlTop"); // NOI18N
        jPnlTop.setLayout(new java.awt.BorderLayout());

        jPnlLabels.setName("jPnlLabels"); // NOI18N
        jPnlLabels.setPreferredSize(new java.awt.Dimension(140, 120));
        jPnlLabels.setLayout(new java.awt.BorderLayout());

        jPnlSeg.setBorder(javax.swing.BorderFactory.createTitledBorder(null, resourceMap.getString("jPnlSeg.border.title"), javax.swing.border.TitledBorder.LEFT, javax.swing.border.TitledBorder.DEFAULT_POSITION, resourceMap.getFont("jPnlSeg.border.titleFont"))); // NOI18N
        jPnlSeg.setName("jPnlSeg"); // NOI18N
        jPnlSeg.setPreferredSize(new java.awt.Dimension(120, 108));

        lblCandidatesNumber.setFont(lblCandidatesNumber.getFont().deriveFont(lblCandidatesNumber.getFont().getSize()-2f));
        lblCandidatesNumber.setText(resourceMap.getString("lblCandidatesNumber.text")); // NOI18N
        lblCandidatesNumber.setName("lblCandidatesNumber"); // NOI18N

        lblTotalSegments.setFont(lblTotalSegments.getFont().deriveFont(lblTotalSegments.getFont().getSize()-2f));
        lblTotalSegments.setText(resourceMap.getString("lblTotalSegments.text")); // NOI18N
        lblTotalSegments.setName("lblTotalSegments"); // NOI18N

        jLblSelected.setFont(jLblSelected.getFont().deriveFont(jLblSelected.getFont().getSize()-2f));
        jLblSelected.setForeground(resourceMap.getColor("jLblSelected.foreground")); // NOI18N
        jLblSelected.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLblSelected.setText(resourceMap.getString("jLblSelected.text")); // NOI18N
        jLblSelected.setName("jLblSelected"); // NOI18N

        jLblTotal.setFont(jLblTotal.getFont().deriveFont(jLblTotal.getFont().getSize()-2f));
        jLblTotal.setForeground(resourceMap.getColor("jLblTotal.foreground")); // NOI18N
        jLblTotal.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLblTotal.setText(resourceMap.getString("jLblTotal.text")); // NOI18N
        jLblTotal.setName("jLblTotal"); // NOI18N

        javax.swing.GroupLayout jPnlSegLayout = new javax.swing.GroupLayout(jPnlSeg);
        jPnlSeg.setLayout(jPnlSegLayout);
        jPnlSegLayout.setHorizontalGroup(
            jPnlSegLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPnlSegLayout.createSequentialGroup()
                .addGap(6, 6, 6)
                .addGroup(jPnlSegLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblTotalSegments)
                    .addComponent(lblCandidatesNumber))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPnlSegLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLblTotal, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLblSelected, javax.swing.GroupLayout.DEFAULT_SIZE, 22, Short.MAX_VALUE))
                .addGap(43, 43, 43))
        );

        jPnlSegLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jLblSelected, jLblTotal});

        jPnlSegLayout.setVerticalGroup(
            jPnlSegLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPnlSegLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPnlSegLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPnlSegLayout.createSequentialGroup()
                        .addComponent(lblTotalSegments)
                        .addGap(18, 18, 18)
                        .addComponent(lblCandidatesNumber))
                    .addGroup(jPnlSegLayout.createSequentialGroup()
                        .addComponent(jLblTotal, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jLblSelected)))
                .addGap(25, 25, 25))
        );

        jPnlLabels.add(jPnlSeg, java.awt.BorderLayout.CENTER);

        jPnlTopCenter.setName("jPnlTopCenter"); // NOI18N
        jPnlTopCenter.setPreferredSize(new java.awt.Dimension(160, 10));

        javax.swing.GroupLayout jPnlTopCenterLayout = new javax.swing.GroupLayout(jPnlTopCenter);
        jPnlTopCenter.setLayout(jPnlTopCenterLayout);
        jPnlTopCenterLayout.setHorizontalGroup(
            jPnlTopCenterLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 158, Short.MAX_VALUE)
        );
        jPnlTopCenterLayout.setVerticalGroup(
            jPnlTopCenterLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 10, Short.MAX_VALUE)
        );

        jPnlLabels.add(jPnlTopCenter, java.awt.BorderLayout.PAGE_START);

        jPnlBottomCenter.setName("jPnlBottomCenter"); // NOI18N
        jPnlBottomCenter.setPreferredSize(new java.awt.Dimension(160, 10));

        javax.swing.GroupLayout jPnlBottomCenterLayout = new javax.swing.GroupLayout(jPnlBottomCenter);
        jPnlBottomCenter.setLayout(jPnlBottomCenterLayout);
        jPnlBottomCenterLayout.setHorizontalGroup(
            jPnlBottomCenterLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 158, Short.MAX_VALUE)
        );
        jPnlBottomCenterLayout.setVerticalGroup(
            jPnlBottomCenterLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 10, Short.MAX_VALUE)
        );

        jPnlLabels.add(jPnlBottomCenter, java.awt.BorderLayout.PAGE_END);

        jPnlRigthCenter.setName("jPnlRigthCenter"); // NOI18N
        jPnlRigthCenter.setPreferredSize(new java.awt.Dimension(5, 100));

        javax.swing.GroupLayout jPnlRigthCenterLayout = new javax.swing.GroupLayout(jPnlRigthCenter);
        jPnlRigthCenter.setLayout(jPnlRigthCenterLayout);
        jPnlRigthCenterLayout.setHorizontalGroup(
            jPnlRigthCenterLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 5, Short.MAX_VALUE)
        );
        jPnlRigthCenterLayout.setVerticalGroup(
            jPnlRigthCenterLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );

        jPnlLabels.add(jPnlRigthCenter, java.awt.BorderLayout.LINE_END);

        jPnlLeftCenter.setName("jPnlLeftCenter"); // NOI18N
        jPnlLeftCenter.setPreferredSize(new java.awt.Dimension(5, 100));

        javax.swing.GroupLayout jPnlLeftCenterLayout = new javax.swing.GroupLayout(jPnlLeftCenter);
        jPnlLeftCenter.setLayout(jPnlLeftCenterLayout);
        jPnlLeftCenterLayout.setHorizontalGroup(
            jPnlLeftCenterLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 5, Short.MAX_VALUE)
        );
        jPnlLeftCenterLayout.setVerticalGroup(
            jPnlLeftCenterLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );

        jPnlLabels.add(jPnlLeftCenter, java.awt.BorderLayout.LINE_START);

        jPnlTop.add(jPnlLabels, java.awt.BorderLayout.SOUTH);

        jPnlBtns.setName("jPnlBtns"); // NOI18N
        jPnlBtns.setPreferredSize(new java.awt.Dimension(140, 90));

        jBtnSegmentMap.setAction(actionMap.get("useMap")); // NOI18N
        jBtnSegmentMap.setFont(jBtnSegmentMap.getFont().deriveFont(jBtnSegmentMap.getFont().getSize()-2f));
        jBtnSegmentMap.setIcon(resourceMap.getIcon("jBtnSegmentMap.icon")); // NOI18N
        jBtnSegmentMap.setText(resourceMap.getString("jBtnSegmentMap.text")); // NOI18N
        jBtnSegmentMap.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jBtnSegmentMap.setMaximumSize(new java.awt.Dimension(120, 35));
        jBtnSegmentMap.setMinimumSize(new java.awt.Dimension(0, 0));
        jBtnSegmentMap.setName("jBtnSegmentMap"); // NOI18N
        jBtnSegmentMap.setPreferredSize(new java.awt.Dimension(120, 35));
        jBtnSegmentMap.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jBtnSegmentMapFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                jBtnSegmentMapFocusLost(evt);
            }
        });
        jPnlBtns.add(jBtnSegmentMap);

        jBtnSegmentFlow.setAction(actionMap.get("useFlow")); // NOI18N
        jBtnSegmentFlow.setFont(jBtnSegmentFlow.getFont().deriveFont(jBtnSegmentFlow.getFont().getSize()-2f));
        jBtnSegmentFlow.setIcon(resourceMap.getIcon("jBtnSegmentFlow.icon")); // NOI18N
        jBtnSegmentFlow.setText(resourceMap.getString("jBtnSegmentFlow.text")); // NOI18N
        jBtnSegmentFlow.setToolTipText(resourceMap.getString("jBtnSegmentFlow.toolTipText")); // NOI18N
        jBtnSegmentFlow.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jBtnSegmentFlow.setMaximumSize(new java.awt.Dimension(120, 35));
        jBtnSegmentFlow.setMinimumSize(new java.awt.Dimension(0, 0));
        jBtnSegmentFlow.setName("jBtnSegmentFlow"); // NOI18N
        jBtnSegmentFlow.setPreferredSize(new java.awt.Dimension(120, 35));
        jBtnSegmentFlow.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jBtnSegmentFlowFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                jBtnSegmentFlowFocusLost(evt);
            }
        });
        jPnlBtns.add(jBtnSegmentFlow);

        jPnlTop.add(jPnlBtns, java.awt.BorderLayout.PAGE_START);

        add(jPnlTop, java.awt.BorderLayout.NORTH);
    }// </editor-fold>//GEN-END:initComponents

    private void jBtnSegmentMapFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jBtnSegmentMapFocusGained
        ButtonHighlighter.highlight(jBtnSegmentMap);
    }//GEN-LAST:event_jBtnSegmentMapFocusGained

    private void jBtnSegmentMapFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jBtnSegmentMapFocusLost
        ButtonHighlighter.unhighlight(jBtnSegmentMap);
    }//GEN-LAST:event_jBtnSegmentMapFocusLost

    private void jBtnSegmentFlowFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jBtnSegmentFlowFocusGained
        ButtonHighlighter.highlight(jBtnSegmentFlow);
    }//GEN-LAST:event_jBtnSegmentFlowFocusGained

    private void jBtnSegmentFlowFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jBtnSegmentFlowFocusLost
        ButtonHighlighter.unhighlight(jBtnSegmentFlow);
    }//GEN-LAST:event_jBtnSegmentFlowFocusLost

    @Action
    public void useMap() {
        this.analysisControl.getFrmAnalysis().getJpnlSegments().showImageMap();
    }

    @Action
    public void useFlow() {
        this.analysisControl.getFrmAnalysis().getJpnlSegments().showImageFlow();
    }

    /**
     * Clear selected segments
     */
    @Action
    public void clearSegments() {
        this.analysisControl.clearSelection();
    }

    public void updatePanel() {
        this.jLblTotal.setText(Integer.toString(this.analysisControl.getAnalysis().getAllSegments().getSegments().size()));
        this.amount = 0;
        this.jLblSelected.setText(this.amount.toString());
    }

    @Action
    public void confirmSelection() {
        this.analysisControl.confirmSelection();
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBtnClear;
    private javax.swing.JButton jBtnCofirm;
    private javax.swing.JButton jBtnSegmentFlow;
    private javax.swing.JButton jBtnSegmentMap;
    private javax.swing.JLabel jLblSelected;
    private javax.swing.JLabel jLblTotal;
    private javax.swing.JPanel jPnlBottom;
    private javax.swing.JPanel jPnlBottomCenter;
    private javax.swing.JPanel jPnlBtns;
    private javax.swing.JPanel jPnlLabels;
    private javax.swing.JPanel jPnlLeftCenter;
    private javax.swing.JPanel jPnlRigthCenter;
    private javax.swing.JPanel jPnlSeg;
    private javax.swing.JPanel jPnlTop;
    private javax.swing.JPanel jPnlTopCenter;
    private javax.swing.JLabel lblCandidatesNumber;
    private javax.swing.JLabel lblTotalSegments;
    // End of variables declaration//GEN-END:variables

    public void update(Observable o, Object arg) {
        if (arg == null) {
            this.amount = 0;
        } else {
            SelectionItem item = (SelectionItem) arg;
            this.amount += ((item.isSelected()) ? 1 : -1);
        }
        this.jLblSelected.setText(this.amount.toString());
    }
}
